package com.grass.plugin.paging.page;

/**
 * 
 * @author WUJXIAO
 * QQ 396246973
 */
@SuppressWarnings("serial")
public class Page implements java.io.Serializable {

    protected int pageSize = 10; // 每页默认10条数据
    protected int currentPage = 1; // 当前页
    protected int totalPages = 0; // 总页数
    protected int totalRows = 0; // 总数据数
    protected int pageStartRow = 0; // 每页的起始行数
    protected int pageEndRow = 0; // 每页显示数据的终止行数
    protected boolean pagination = false;   //是否分页
    boolean hasNextPage = false; // 是否有下一页
    boolean hasPreviousPage = false; // 是否有前一页

    protected Page() {
    }

    /**
     * 构造器
     *
     * @param rows 总记录数
     * @param pageSize 每页记录数
     */
    protected Page(int rows, int pageSize) {
        init(rows, pageSize);
    }

    /**
     * 初始化分页参数:需要先设置totalRows
     *
     * @param rows 总记录数
     * @param pageSize 每页记录数
     */
    private void init(int rows, int size) {
        this.totalRows = rows <= 0 ? 1 : rows;
        this.pageSize = size <= 0 ? this.totalRows : size;
        if ((totalRows % this.pageSize) == 0) {
            totalPages = totalRows / pageSize;
        } else {
            totalPages = totalRows / pageSize + 1;
        }
    }

    public void init(int rows, int pageSize, int currentPage) {
        init(rows, pageSize);
        this.currentPage = currentPage <= 0 ? 1 : currentPage;
        calculatePage();
    }

    /**
     * 计算当前页的取值范围：pageStartRow和pageEndRow
     */
    private void calculatePage() {
        if ((currentPage - 1) > 0) {
            hasPreviousPage = true;
        } else {
            hasPreviousPage = false;
        }
        if (currentPage >= totalPages) {
            hasNextPage = false;
        } else {
            hasNextPage = true;
        }
        if (currentPage * pageSize < totalRows) { // 判断是否为最后一页
            pageEndRow = currentPage * pageSize;
            pageStartRow = pageEndRow - pageSize;
        } else {
            pageEndRow = totalRows;
            pageStartRow = pageSize * (totalPages - 1);
        }
    }


    /**
     * @return
     */
    public int getCurrentPage() {
        return currentPage;
    }

    /**
     * @return
     */
    public boolean isHasNextPage() {
        return hasNextPage;
    }

    /**
     * @return
     */
    public boolean isHasPreviousPage() {
        return hasPreviousPage;
    }

    /**
     * @return
     */
    public int getPageEndRow() {
        return pageEndRow;
    }

    /**
     * @return
     */
    public int getPageSize() {
        return pageSize;
    }

    /**
     * @return
     */
    public int getPageStartRow() {
        return pageStartRow;
    }

    /**
     * @return
     */
    public int getTotalPages() {
        return totalPages;
    }

    /**
     * @return
     */
    public int getTotalRows() {
        return totalRows;
    }

    
    public boolean isPagination() {
        return pagination;
    }

    public void setPagination(boolean pagination) {
        this.pagination = pagination;
    }
}
